package lock.reentrantlock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Brook
 */
public class CinemaBookSeat {

    private static ReentrantLock reentrantLock = new ReentrantLock();
    static Object o1 = new Object();
    static Object o2 = new Object();

    private static void bookSeat() {
        reentrantLock.lock();
        try {
            synchronized (o1) {
                System.out.println(Thread.currentThread().getName()+ "开始预定座位");
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName()+"完成座位预定");
            }
        }catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            reentrantLock.unlock();
        }
    }

    public static void main(String[] args) {
        new Thread(() -> bookSeat()).start();
        new Thread(() -> bookSeat()).start();
        new Thread(() -> bookSeat()).start();
        new Thread(() -> bookSeat()).start();
    }
}
